﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.Core.Authentication;
using MP.Core.Library;
using MP.Core.Config;

using MP.LayerHelper.MP_OBS.BLL;

public partial class Member_Control_Menu : System.Web.UI.UserControl
{
    public string MenuId = "MEMBER";

    //MenuService smenu = (MenuService)WebUtility.GetWebService(EnumServices.CRM, "ManagerService/MenuService.asmx", typeof(MenuService), "DataLayer.MenuService");
    //MenuService smenu = new MenuService();

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
    }

    void LoadData()
    {
        DataSet dst = MenuManager.CreateInstant().GetMenuByParentId(MenuId);
        divMember.Visible = false;
        if (dst != null && dst.Tables.Count > 0)
        {
            DataTable menu = MPRoleProvider.CreateInstant().CheckRolesMenu(Util.CurrentUserName, dst.Tables[0]);
            if (menu != null && menu.Rows.Count > 0)
            {
                divMember.Visible = true;
                repMenu1.DataSource = menu;
                repMenu1.DataBind();
            }
        }
    }

    public DataTable getMenu(string menuId)
    {
        DataSet dst = MenuManager.CreateInstant().GetMenuByParentId(menuId);
        if (dst != null && dst.Tables.Count > 0)
        {
            DataTable menu = MPRoleProvider.CreateInstant().CheckRolesMenu(Util.CurrentUserName, dst.Tables[0]);
            return menu;
        }
        return null;
    }

    public string Show(string id)
    {
        DataTable tbl = getMenu(id);
        if (tbl == null)
        {
            return "display:none;";
        }
        return "";
    }

    protected void repMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView view = (DataRowView)e.Item.DataItem;
        Repeater rep = (Repeater)e.Item.FindControl("repSub");

        bool isview = false;

        if (rep != null)
        {
            DataTable tbl = getMenu(view["Id"].ToString());
            if (tbl != null && tbl.Rows.Count > 0)
            {
                rep.DataSource = tbl;
                rep.DataBind();

                isview = true;
            }
        }

        if (view["Action"].ToString() != ".")
            isview = true;

        e.Item.Visible = isview;
    }
}
